package com.pioneer.dao;

import ldap.*;
import com.pioneer.util.AppProperties;
import com.pioneer.beans.ContactBean;

import java.util.List;
import java.util.Iterator;
import java.util.Properties;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.NameAlreadyBoundException;
import javax.naming.CommunicationException;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.SearchResult;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;


/**
 * Title:        Pioneer Application
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Pioneer Metals
 * @author Gary Chen
 * @version 1.0
 */

public class LdapContactDAO
implements ContactDAO{

	private LDAPConnector ldap;

	public LdapContactDAO(Properties initProps,String ldapContext)
		throws DataAccessException{
		try {
			Properties props = new Properties(initProps);
			props.setProperty(AppProperties.PEOPLE_CONTEXT,ldapContext);
			ldap = new LDAPConnector(props);
		}
		catch (LDAPException ex) {
			throw new DataAccessException(ex.getMessage());
		}
	}	
	
	public void addContact(ContactBean bean)
	throws DataAccessException{
		try {
			ldap.accessContact(toContact(bean),LDAPConnector.ADD);
		}
		catch (LDAPException ex) {
			throw new DataAccessException(ex.getMessage());
		}
	}
	
	public void editContact(ContactBean bean, ContactBean oldBean)
	throws DataAccessException{
		try {
			Contact queryContact = new Contact();
			String oldCn = oldBean.getCn();
			Contact contact = toContact(bean);
			queryContact.setCn(bean.getCn());
			ldap.accessContact(queryContact,LDAPConnector.SEARCH);		
			List contactList = ldap.getSearchResults();
			if ((contactList==null) || (contactList.size()<1)) {
				ldap.accessContact(contact, LDAPConnector.ADD);
				queryContact.setCn(oldCn);
				ldap.accessContact(queryContact, LDAPConnector.DELETE);
			}
			else {
				if ((oldCn.equals(bean.getCn()))){
						ldap.accessContact(contact,LDAPConnector.EDIT);
				}
				else {
					throw new DataAccessException("The unique name you entered is already in the directory.  Please create a new one.");
				}				
			}
		}
		catch (LDAPException ex) {
			throw new DataAccessException(ex.getMessage());
		}
		
	}
	
	public void deleteContact(ContactBean bean)
	throws DataAccessException{
		try {
			ldap.accessContact(toContact(bean),LDAPConnector.DELETE);
		}
		catch (LDAPException ex) {
			throw new DataAccessException(ex.getMessage());
		}
	}
	
	public List findContacts(ContactBean bean)
	throws DataAccessException{
		List contactBeanList = new ArrayList();
		try {
			ldap.accessContact(toContact(bean),LDAPConnector.GET_CN_LIST);
			List contactList = ldap.getSearchResults();
			Iterator it = contactList.iterator();
			while (it.hasNext()) {
				Contact nextContact = (Contact)it.next();				
				contactBeanList.add(toContactBean(nextContact));				
			}		
		}
		catch (LDAPException ex) {
			throw new DataAccessException(ex.getMessage());
		}
		return contactBeanList;
	}
	
	public ContactBean findContact(String cn)
	throws DataAccessException{
		List contactBeanList = new ArrayList();
		ContactBean bean = new ContactBean();
		bean.setCn(cn);
		try {			
			ldap.accessContact(toContact(bean),LDAPConnector.SEARCH);
			List contactList = ldap.getSearchResults();
			Iterator it = contactList.iterator();
			while (it.hasNext()) {
				Contact nextContact = (Contact)it.next();				
				contactBeanList.add(toContactBean(nextContact));				
			}
			if (contactBeanList.size()>0) {
				return (ContactBean)contactBeanList.get(0);
			}
		}
		catch (LDAPException ex) {
			throw new DataAccessException(ex.getMessage());
		}		
		return bean;
	}
	
	private Contact toContact(ContactBean bean){
		Contact contact = new Contact();
		contact.setCn(bean.getCn());		
		contact.setEmail(bean.getEmail());
		contact.setBusinessPhone(bean.getBusinessPhone());
		contact.setBusinessFax(bean.getBusinessFax());
		contact.setBusinessAddress(bean.getBusinessAddress());
		contact.setMobile(bean.getMobile());
		contact.setHomeAddr(bean.getHomeAddr());
		contact.setBusinessCity(bean.getBusinessCity());
		contact.setBusinessState(bean.getBusinessState());
		contact.setCompany(bean.getCompany());
		contact.setDepartment(bean.getDepartment());		
		contact.setFirstName(bean.getFirstName());
		contact.setHomePhone(bean.getHomePhone());
		contact.setJobTitle(bean.getJobTitle());
		contact.setLastName(bean.getLastName());		
		contact.setBusinessZip(bean.getBusinessZip());
		return contact;
	}
	
	private ContactBean toContactBean(Contact contact) {		
		ContactBean contactBean = new ContactBean();
		contactBean.setCn(contact.getCn());
		contactBean.setEmail(contact.getEmail());
		contactBean.setBusinessPhone(contact.getBusinessPhone());
		contactBean.setBusinessFax(contact.getBusinessFax());
		contactBean.setBusinessAddress(contact.getBusinessAddress());
		contactBean.setMobile(contact.getMobile());
		contactBean.setHomeAddr(contact.getHomeAddr());
		contactBean.setBusinessCity(contact.getBusinessCity());
		contactBean.setBusinessState(contact.getBusinessState());
		contactBean.setCompany(contact.getCompany());
		contactBean.setDepartment(contact.getDepartment());
		contactBean.setFirstName(contact.getFirstName());
		contactBean.setHomePhone(contact.getHomePhone());
		contactBean.setJobTitle(contact.getJobTitle());
		contactBean.setLastName(contact.getLastName());
		contactBean.setBusinessZip(contact.getBusinessZip());
		return contactBean;
	}	
	
	public static void main(String args[]) {
		try {
			Properties prop = new Properties();
			prop.load(new java.io.FileInputStream("D:\\documents\\development\\ldap\\webapps\\WEB-INF\\ldap.prop"));
			ContactDAO dao = new LdapContactDAO(prop,"MetalsApp");
			ContactBean contact = new ContactBean();
			//contact.setCn("Ga");
			contact.setDepartment("Human Genetics");
			List contactList = dao.findContacts(contact);
			Iterator it = contactList.iterator();
			while (it.hasNext()) {
				ContactBean nextContact = (ContactBean)it.next();
				System.out.println("The cn is: "+nextContact.getCn());
				System.out.println("The email is: "+nextContact.getEmail());
			}
	
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
	}
	
}